package durak.client.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import durak.common.model.CommonPlayerData;
import durak.common.model.GameTableState;

public class ExitScreen extends JFrame {

	private JPanel exitPanel;
	private GameTableState gameTableState;
	String text;
	JLabel          smallPlayerIcon;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ExitScreen frame = new ExitScreen();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ExitScreen() {
		setTitle("goodBye!  " );
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		exitPanel = new JPanel();
		exitPanel.setBackground(new Color(34, 139, 34));
		exitPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(exitPanel);
		exitPanel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Big Durak is " + whoIsTheDurak());
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(53, 78, 305, 94);
		exitPanel.add(lblNewLabel);
		
		JButton btnClickToExit = new JButton("Click to Exit");
		btnClickToExit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				System.exit(0);
			}
		});
		btnClickToExit.setBounds(150, 201, 89, 23);
		exitPanel.add(btnClickToExit);
		
		JLabel lblThankYouFor = new JLabel("Thank you for Playing !");
		lblThankYouFor.setForeground(new Color(255, 255, 0));
		lblThankYouFor.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblThankYouFor.setHorizontalAlignment(SwingConstants.CENTER);
		lblThankYouFor.setBounds(117, 25, 167, 23);
		exitPanel.add(lblThankYouFor);

		
	}
	
	
	public String whoIsTheDurak()
	{
		int highestScore = 0;
		String bigLoser = "";
		
		 for (CommonPlayerData player : gameTableState.getPlayers()) {
	            if (player.getLoses() > highestScore) 
	            {
	                    highestScore = player.getLoses();
	                    bigLoser = player.getName();
	            }
	    }
		return bigLoser; 
	}
}
